package com.serkol.ams.controller.api;



import com.serkol.ams.bean.Result;
import com.serkol.ams.entity.SysConfig;
import com.serkol.ams.service.SettingsService;
import com.serkol.ams.service.SysConfigService;
import com.serkol.annotation.IsLogin;
import com.serkol.constant.ResultConstant;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Hesenjan(Serkol)
 * @since 2021-11-17
 */

@RestController
@RequestMapping("api/cdn/sys/config")
public class SysConfigApiController {
    @Autowired
    private SysConfigService service;

    @Value("${agentType}")
    private int agentType;


    @IsLogin
    @GetMapping("findByList")
    public Result list() {
        List<SysConfig> list = service.list();
        return !list.isEmpty() ? Result.ok(list) : Result.failure(ResultConstant.EMPTIY);
    }

    @ApiOperation(value = "获取参数值")
    @GetMapping("info")
    public Result info() {
        Map<String, Object> list = service.findByKeys();
        if(list==null){
            list=new HashMap<>();
        }
        list.put("agentType",agentType);

        return !list.isEmpty() ? Result.ok(list) : Result.failure(ResultConstant.EMPTIY);
    }



    @IsLogin
    @PostMapping("edit")
    public Result edit(@RequestBody SysConfig data) {



            boolean flag = false;
        if (data.getId() != null && data.getId() > 0) {

                flag = service.updateById(data);
            } else {
                flag = service.save(data);
            }

            return flag ? Result.ok() : Result.failure(ResultConstant.FAIL);


    }





}

